package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.store.Question;
import com.itheima.mapper.store.QuestionMapper;
import com.itheima.service.QuestionService;
import com.itheima.service.QuestionService;
import com.itheima.utils.MapperUtil;
import com.itheima.utils.UidUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

//QuestionService实现类
public class QuestionServiceImpl implements QuestionService {
    /**
     * 查找所有
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<Question> findAll(String pageNum,String pageSize) {
        SqlSession session = MapperUtil.getSession();
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);
        int pageN=1;
        int pageS=10;
        if(StringUtils.isNotBlank(pageNum)){
            pageN=Integer.parseInt(pageNum);
        }
        if(StringUtils.isNotBlank(pageSize)){
            pageS=Integer.parseInt(pageSize);
        }
        PageHelper.startPage(pageN,pageS);
        List<Question> list=mapper.findAll();
        MapperUtil.close(session);
        return list;
    }

    /**
     * 新增(其中ID(string类型)用UUID对象生成)
     * @param question
     */
    @Override
    public String save(Question question,boolean flag) {
        SqlSession session = MapperUtil.getSession();
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);
        //创建时间,默认审核状态,id
        String uid = UidUtil.getUid();
        question.setReviewStatus("0");
        question.setId(uid);
        question.setCreateTime(new Date());
        //flag为true则记录文件名
        if(flag){
            //设置当前存储图片名称与ID值相同
            question.setPicture(uid);
        }

        mapper.save(question);
        MapperUtil.close(session);
        return uid;
    }

    /**
     * 根据多ID删除
     * @param id
     */
    @Override
    public boolean deleteByIds(String id) {
        SqlSession session=null;
        boolean flag=false;//因为有外键关联,所以可能会删除失败
        try {
            session = MapperUtil.getSession();
            QuestionMapper mapper = session.getMapper(QuestionMapper.class);
            String[] idArray = id.split(",");
            mapper.deleteByIds(idArray);
            flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MapperUtil.close(session);
        }
        return flag;
    }

    /**
     * 根据id查找对象
     * @param id
     * @return
     */
    @Override
    public Question findById(String id) {
        SqlSession session = MapperUtil.getSession();
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);
        Question question=mapper.findById(id);
        MapperUtil.close(session);
        return question;
    }

    /**
     * 更新对象数据
     * @param question
     */
    @Override
    public void update(Question question,boolean flag) {
        //1、获取sesssion
        SqlSession session = MapperUtil.getSession();
        //2、获取代理对象
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);
        //flag为true则记录文件名
        if(flag){
            //设置当前存储图片名称与ID值相同
            question.setPicture(question.getId());
        }

        mapper.update(question);
        //3、关闭资源
        MapperUtil.close(session);

    }
}
